package com.imooc.pan.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imooc.pan.entity.PayRecord;
import com.imooc.pan.entity.StorageOrder;
import com.imooc.pan.feignservice.UserStorageSizeServiceClient;
import com.imooc.pan.mapper.PayRecordMapper;
import com.imooc.pan.service.PayRecordService;
import com.imooc.pan.service.StorageOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
* @author Janson
* @description 针对表【pay_record】的数据库操作Service实现
* @createDate 2023-07-31 20:39:53
*/
@Service
public class PayRecordServiceImpl extends ServiceImpl<PayRecordMapper, PayRecord>
    implements PayRecordService{
    
    @Autowired
    private StorageOrderService storageOrderService;
    
    @Autowired
    private UserStorageSizeServiceClient userStorageSizeServiceClient;
    
    /*@Autowired
    private RPanUserStorageService rPanUserStorageService;*/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveRecond(PayRecord payRecord) {

        // 2.更新用户订单状态
        // 设置支付状态 （1：已支付， 0：未支付）
        
        LambdaQueryWrapper<StorageOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StorageOrder::getPayNo, payRecord.getPayNo());
        // 根据订单号 查询 订单
        StorageOrder storageOrder = storageOrderService.getOne(queryWrapper);
        if (Objects.isNull(storageOrder)){
            throw new RuntimeException("订单不存在");
        }
        // 设置支付状态 （1：已支付， 0：未支付）
        storageOrder.setStatus("1");
        storageOrderService.updateById(storageOrder);
    
        // 1.首先保存支付记录
        Long userId = storageOrder.getUserId();
        payRecord.setUserId(String.valueOf(userId));
        save(payRecord);
    
        // 3.更新用户存储空间
        userStorageSizeServiceClient.updateUserStorage(storageOrder.getBusinessSize(),
                                                       storageOrder.getBusinessDuration());
        /*// 构造更新条件
        LambdaQueryWrapper<RPanUserStorage> userStorageWrapper = new LambdaQueryWrapper<>();
        userStorageWrapper.eq(RPanUserStorage::getUserId,userId);
        RPanUserStorage userStorage = rPanUserStorageService.getOne(userStorageWrapper);
        // 获取用户之前购买的存储空间，若每购买，则为0
        BigDecimal oldUserBuyStorageSize = userStorage.getUserBuyStorageSize();
        // 当前订单购买的存储空间
        BigDecimal curBuySize = storageOrder.getBusinessSize();
        BigDecimal newStorageSize = curBuySize.add(oldUserBuyStorageSize);
        
        // 设置新的购买存储
        userStorage.setUserBuyStorageSize(newStorageSize);
        // 计算购买存储套餐的过期时间
        Integer duration = storageOrder.getBusinessDuration();
        DateTime empiredDateTime = DateUtil.offsetMonth(DateUtil.date(), duration);
        userStorage.setUserBuyEpiredTime(empiredDateTime);
        
        rPanUserStorageService.updateById(userStorage);*/
    }
}




